package cn.lblbc.appstore.view

import android.content.Context
import android.icu.lang.UCharacter.GraphemeClusterBreak.L
import android.util.AttributeSet
import android.util.Log
import cn.lblbc.appstore.module.download.DownloadFileInfo
import cn.lblbc.appstore.module.download.DownloadStatus
import cn.lblbc.appstore.module.download.StatusInfo
import cn.lblbc.appstore.utils.AppUtils.openApp

class InstallButton(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatTextView(context, attrs) {
    private var mStatusInfo: StatusInfo? = null
    private lateinit var mPackageName: String
    private var mDownloadListener: DownloadListener? = null

    init {
        setOnClickListener {
            if (mStatusInfo != null) {
                var statusInfo = mStatusInfo!!
                if (!statusInfo.isInstalling) {
                    if (isDownloadingOrWaiting(statusInfo)) {
                        mDownloadListener?.onDownloadPaused()
                    } else if (isLocalAppNewest()) {
                        openApp(mPackageName)
                    } else {
                        mDownloadListener?.onDownloadStarted()
                    }
                }
            }
        }
    }

    /**
     * 本地是否是最新版本
     */
    private fun isLocalAppNewest(): Boolean {
        var result = false
        val serverAppInfo = mStatusInfo?.serverAppInfo
        val localAppInfo = mStatusInfo?.localAppInfo
        if (localAppInfo != null) {
            if (serverAppInfo != null) {
                if (localAppInfo.versionCode >= serverAppInfo.versionCode) {
                    result = true
                }
            } else {
                result = true
            }
        }
        return result
    }

    fun setStatus(statusInfo: StatusInfo) {
        mStatusInfo = statusInfo
        if (statusInfo.isInstalling) {
            text = "安装中"
        } else {
            if (statusInfo.downloadFileInfo != null) {
                processDownloadFileInfo(statusInfo.downloadFileInfo!!)
            } else {
                processServerAndLocalAppInfo(statusInfo)
            }
        }
    }

    fun setDownloadListener(downloadListener: DownloadListener) {
        mDownloadListener = downloadListener
    }

    private fun isDownloadingOrWaiting(statusInfo: StatusInfo): Boolean {
        var result = false
        if (statusInfo.downloadFileInfo != null) {
            val downloadFileInfo = statusInfo.downloadFileInfo!!
            when (downloadFileInfo.downloadStatus) {
                DownloadStatus.STATUS_WAITING -> result = true
                DownloadStatus.STATUS_DOWNLOADING -> result = true
                DownloadStatus.STATUS_CONNECT -> result = true
                else -> {}
            }
        }
        return result
    }

    private fun processServerAndLocalAppInfo(statusInfo: StatusInfo) {
        if (statusInfo.serverAppInfo != null) {
            val serverAppInfo = statusInfo.serverAppInfo!!
            val localAppInfo = statusInfo.localAppInfo
            if (localAppInfo != null) {
                if (localAppInfo.versionCode < serverAppInfo.versionCode) {
                    text = "更新"
                } else {
                    text = "打开"
                }
            } else {
                text = "安装"
            }
        } else {
            if (statusInfo.localAppInfo != null) {
                text = "打开"
            } else {
                text = "安装"
            }
        }
    }

    private fun processDownloadFileInfo(it: DownloadFileInfo) {
        when (it.downloadStatus) {
            DownloadStatus.STATUS_DOWNLOADING -> {
                val progress = (it.downloadedSize * 100 / it.fileSize).toInt()
                text = "$progress%"
            }
            DownloadStatus.STATUS_APPOINT -> text = "继续"
            DownloadStatus.STATUS_WAITING -> text = "等待中"
            DownloadStatus.STATUS_CONNECT -> text = "连接中"
            DownloadStatus.STATUS_PAUSE -> text = "继续"
            DownloadStatus.STATUS_FAILURE -> text = "继续"
            else -> {}
        }
    }

    fun setPackageName(packageName: String) {
        mPackageName = packageName
    }

    interface DownloadListener {
        fun onDownloadStarted()
        fun onDownloadPaused()
    }
}